﻿using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Xpedite.Resources;
using Xpedite.WebAbstraction;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Test.Resources
{
  [TestClass]
  public class ResourceCacheTest
  {
    private const String RootRelativePath = "/SomeResource.ext";

    private readonly Mock<IFakeResourceFactory> _fakeFactory = new Mock<IFakeResourceFactory>(MockBehavior.Strict);
    private readonly Mock<IResourceContainer> _resource = new Mock<IResourceContainer>(MockBehavior.Strict);
    private readonly Mock<IResourceUrl> _resourceUrl = new Mock<IResourceUrl>(MockBehavior.Strict);
    private readonly Mock<IWebCache> _cache = new Mock<IWebCache>(MockBehavior.Strict);
    private readonly ResourceCache _resourceCache;

    public ResourceCacheTest()
    {
      _resourceCache = new ResourceCache(_cache.Object);
      _resourceUrl.SetupGet(mock => mock.VirtualPath).Returns(RootRelativePath);
    }

    [TestMethod]
    public void TestGetWhereResourceFound()
    {
      _cache.Setup(mock => mock.Get(RootRelativePath)).Returns(_resource.Object);

      Assert.AreSame(_resource.Object, _resourceCache.Get(_resourceUrl.Object));
    }

    [TestMethod]
    public void TestGetWhereResourceNotFound()
    {
      _cache.Setup(mock => mock.Get(RootRelativePath)).Returns((IResourceContainer)null);

      Assert.IsNull(_resourceCache.Get(_resourceUrl.Object));
    }

    [TestMethod]
    public void TestGetOrCreateWithNoDependencyWhereResourceFound()
    {
      _cache.Setup(mock => mock.Get(RootRelativePath)).Returns(_resource.Object);

      Assert.AreSame(_resource.Object, _resourceCache.GetOrCreateWithNoDependency(_resourceUrl.Object, _fakeFactory.Object.CreateResource));
    }

    [TestMethod]
    public void TestGetOrCreateWithNoDependencyWhereResourceNotFound()
    {
      _cache.Setup(mock => mock.Get(RootRelativePath)).Returns((IResourceContainer)null);
      _cache.Setup(mock => mock.AddWithNoDependency(RootRelativePath, _resource.Object));
      _fakeFactory.Setup(mock => mock.CreateResource()).Returns(_resource.Object);

      Assert.AreSame(_resource.Object, _resourceCache.GetOrCreateWithNoDependency(_resourceUrl.Object, _fakeFactory.Object.CreateResource));
    }

    [TestMethod]
    public void TestGetOrCreateWithFileDependencyWhereResourceFound()
    {
      String path = Path.GetTempFileName();

      try
      {
        _cache.Setup(mock => mock.Get(RootRelativePath)).Returns(_resource.Object);

        Assert.AreSame(_resource.Object, _resourceCache.GetOrCreateWithFileDependency(_resourceUrl.Object, _fakeFactory.Object.CreateResource, path));
      }
      finally
      {
        File.Delete(path);
      }
    }

    [TestMethod]
    public void TestGetOrCreateWithFileDependencyWhereResourceNotFoundAndFileExists()
    {
      String path = Path.GetTempFileName();

      try
      {
        _cache.Setup(mock => mock.Get(RootRelativePath)).Returns((IResourceContainer)null);
        _cache.Setup(mock => mock.AddWithFileDependency(RootRelativePath, _resource.Object, path));
        _fakeFactory.Setup(mock => mock.CreateResource()).Returns(_resource.Object);

        Assert.AreSame(_resource.Object, _resourceCache.GetOrCreateWithFileDependency(_resourceUrl.Object, _fakeFactory.Object.CreateResource, path));
      }
      finally
      {
        File.Delete(path);
      }
    }

    [TestMethod]
    public void TestGetOrCreateWithFileDependencyWhereResourceNotFoundAndFileDoesNotExists()
    {
      String path = Path.GetTempFileName();
      File.Delete(path);

      _cache.Setup(mock => mock.Get(RootRelativePath)).Returns((IResourceContainer)null);
      _cache.Setup(mock => mock.AddWithNoDependency(RootRelativePath, _resource.Object));
      _fakeFactory.Setup(mock => mock.CreateResource()).Returns(_resource.Object);

      Assert.AreSame(_resource.Object, _resourceCache.GetOrCreateWithFileDependency(_resourceUrl.Object, _fakeFactory.Object.CreateResource, path));
    }

    [TestMethod]
    public void TestGetOrCreateWithResourceDependenciesWhereResourceFound()
    {
      var resourceDependencies = new IResourceContainer[0];

      _cache.Setup(mock => mock.Get(RootRelativePath)).Returns(_resource.Object);

      Assert.AreSame(_resource.Object, _resourceCache.GetOrCreateWithResourceDependencies(_resourceUrl.Object, _fakeFactory.Object.CreateResource, resourceDependencies));
    }

    [TestMethod]
    public void TestGetOrCreateWithResourceDependenciesWhereResourceNotFound()
    {
      var _resource = new Mock<IResourceContainer>(MockBehavior.Strict);
      var resourceDependencies = new [] { _resource.Object };

      _resource.SetupGet(mock => mock.ResourceUrl).Returns(_resourceUrl.Object);
      _cache.Setup(mock => mock.Get(RootRelativePath)).Returns((IResourceContainer)null);
      _cache.Setup(mock => mock.AddWithCacheDependencies(RootRelativePath, _resource.Object, It.Is((String[] value) => value.Length == 1 && value[0] == RootRelativePath)));
      _fakeFactory.Setup(mock => mock.CreateResource()).Returns(_resource.Object);

      Assert.AreSame(_resource.Object, _resourceCache.GetOrCreateWithResourceDependencies(_resourceUrl.Object, _fakeFactory.Object.CreateResource, resourceDependencies));
    }
    
    [TestMethod]
    public void TestRemove()
    {
      _cache.Setup(mock => mock.Remove(RootRelativePath));

      _resourceCache.Remove(_resourceUrl.Object);
    }

    internal interface IFakeResourceFactory
    {
      IResourceContainer CreateResource();
    }
  }
}
